package others.swing.p024;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

class One extends JFrame implements ActionListener {
    JLabel jlabel1, jlabel2;
    JButton jbutton1, jbutton2;
    JTextField jtextfield1;
    JPanel jpanel1;
    JPasswordField passwordText;

    One() {
        jpanel1 = new JPanel();
        jlabel1 = new JLabel("用户名:");
        jlabel2 = new JLabel("密 码:");
        jbutton1 = new JButton("登录");
        jbutton2 = new JButton("取消");
        Font f = new Font("楷体_GB2312", Font.BOLD, 32);
        jlabel1.setFont(f);
        jlabel2.setFont(f);
        jtextfield1 = new JTextField(10);
        passwordText = new JPasswordField(10);
        passwordText.setEchoChar('*');
        jbutton1.addActionListener(this);
        jbutton2.addActionListener(this);
        setLayout(new FlowLayout());
        jpanel1.add(jbutton1);
        jpanel1.add(jbutton2);
        add(jlabel1);
        add(jtextfield1);
        add(jlabel2);
        add(passwordText);
        add(jpanel1);
        setBounds(100, 100, 500, 500);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jbutton1) {
            String str1, str2;
            str1 = jtextfield1.getText().trim();
            str2 = String.valueOf(passwordText.getPassword());
            Connection con;
            Statement sql;
            ResultSet rs;
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException ee) {
                System.out.println("" + ee);
            }
            try {
                con = DriverManager.getConnection(Main.DB_URL, Main.DB_USER_NAME, Main.DB_PASSWORD);
                sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                rs = sql.executeQuery("SELECT * FROM 用户");
                boolean boo = true;
                while (rs.next()) {
                    String 用户编号 = rs.getString("用户编号");
                    String 用户名 = rs.getString("用户名");
                    if (用户名.trim().equals(str1) && 用户编号.trim().equals(str2)) {
                        JOptionPane.showMessageDialog(this, "欢迎进入图书管理系统", "login", JOptionPane.PLAIN_MESSAGE);
                        setVisible(false);
                        FrameMain t = new FrameMain();
                        boo = false;
                    }
                }
                if (boo) {
                    JOptionPane.showMessageDialog(this, "用户不存在或者输入错误请重新输入！", "提示", JOptionPane.WARNING_MESSAGE);
                    jtextfield1.setText("");
                    passwordText.setText("");
                }
                con.close();
            } catch (SQLException eee) {
                System.out.println(eee);
            }
        } else {
            System.exit(0);
        }
    }
}
